package com.example.demo2.mapper;

import com.example.demo2.entity.*;
import jakarta.transaction.Transactional;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {
    @Select("select * from orders where uID=#{uID}")
    public List<Orders> findOrdersByUID(@Param("uID") Integer uID);

    @Delete("delete from orders where oId=#{oId}")
    public int deleteOrdersByOID(@Param("oId") Integer oId);
    @Select("SELECT od.odId, s.staName, p.pjName,u.uRealName, e.eName, so.soName, o.oStatus " +
            "FROM orders o " +
            "JOIN orders_detail od ON o.oId = od.oId " +
            "JOIN project p ON od.pjId = p.pjId " +
            "JOIN standard s ON p.staId = s.staId " +
            "JOIN equipment e ON od.eId = e.eId " +
            "JOIN users u ON u.uID = o.uID " +
            "JOIN solution so ON od.soId = so.soId " +
            "WHERE o.oStatus = '计划采购'")
    List<Map<String, Object>> getOrdersByStatus();





    // 更新设备状态为“购置完成”
    @Update("UPDATE orders SET oStatus = '采购完成' WHERE oId = #{oId}")
    void updateOrderToCompleted(int oId);

    // 获取所有状态为 "购置完成" 的订单
    @Select("SELECT od.odId, s.staName, p.pjName, u.uRealName, e.eName, so.soName, o.oStatus " +
            "FROM orders o " +
            "JOIN orders_detail od ON o.oId = od.oId " +
            "JOIN project p ON od.pjId = p.pjId " +
            "JOIN standard s ON p.staId = s.staId " +
            "JOIN equipment e ON od.eId = e.eId " +
            "JOIN users u ON u.uID = o.uID " +
            "JOIN solution so ON od.soId = so.soId " +
            "WHERE o.oStatus = '采购完成'")
    List<Map<String, Object>> getCompletedOrders();



    // 删除指定订单及其对应的详情记录
    @Delete("DELETE FROM orders_detail WHERE oId = #{oId}")
    void deleteOrderDetailByOrderId(int oId);

    @Delete("DELETE FROM orders WHERE oId = #{oId}")
    void deleteOrderById(int oId);



    // 删除 orders_detail 表中的记录
    @Transactional
    @DeleteProvider(type = SqlProvider.class, method = "deleteOrdersDetailByIds")
    void deleteOrdersDetailByIds(@Param("oIds") List<Integer> oIds);

    // 删除 orders 表中的记录
    @Transactional
    @DeleteProvider(type = SqlProvider.class, method = "deleteOrdersByIds")
    void deleteOrdersByIds(@Param("oIds") List<Integer> oIds);

    // 提供动态 SQL 的内部类
    class SqlProvider {
        // 删除 orders_detail 表
        public String deleteOrdersDetailByIds(Map<String, Object> params) {
            List<Integer> ids = (List<Integer>) params.get("oIds");
            StringBuilder sql = new StringBuilder();
            sql.append("DELETE FROM orders_detail WHERE oId IN (");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));
                if (i < ids.size() - 1) {
                    sql.append(",");
                }
            }
            sql.append(")");
            return sql.toString();
        }

        // 删除 orders 表
        public String deleteOrdersByIds(Map<String, Object> params) {
            List<Integer> ids = (List<Integer>) params.get("oIds");
            StringBuilder sql = new StringBuilder();
            sql.append("DELETE FROM orders WHERE oId IN (");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));
                if (i < ids.size() - 1) {
                    sql.append(",");
                }
            }
            sql.append(")");
            return sql.toString();
        }
    }







}